<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="root">

    </div>
</body>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<!-- 用来将jsx语法转化为js语法 -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.js"></script>
<script type="text/babel">
    class App extends React.Component {
        state = {
            arr: [
                {
                    name: "小明",
                    age: '你好'
                }
            ]
        }
        push = (name, add) => {
            //子组件给父组件传值
            this.state.arr.push({
                name: name,
                age: add
            })
            // 更新数据
            this.setState({
                arr: this.state.arr
            })
        }
        bost = (index) => {
            this.state.arr.splice(index, 1)
            this.setState({
                arr: this.state.arr
            })
        }
        render() {
            return (
                <div>
                    {
                        //父组件传子
                    }
                    <Com1 push={this.push} />
                    {
                        // 1. 把arr传子<Com2>
                    }
                    <Com2 arr={this.state.arr} bost={this.bost} />
                </div>
            )
        }
    }
    class Com1 extends React.Component {
        state = {
            name: '',
            add: ''
        }
        render() {
            //子组件接收此函数
            let { push } = this.props
            let { name, add } = this.state
            return (
                <div>
                    <div>评论人：
                        <br />
                        {
                            // onChange 监听input数据 e
                        }
                        <input type="text" onChange={(e) => {
                            this.setState({
                                name: e.target.value
                            })
                        }} value={this.state.name} /> </div>
                    <div>
                        评论内容：
                        <br />
                        {
                            // onChage 监听input里的数据 e
                        }
                        <textarea cols='30' rows='10' onChange={(e) => {
                            this.setState({
                                // 得到输入框的内容
                                add: e.target.value
                            })
                        }} value={this.state.add}>
                        </textarea>
                        <br />
                        <button onClick={() => {
                            //点击的时候调用父组件传递过来的函数  
                            // 用这个函数给父组件传递参数
                            push(name, add)
                        }}>发表
                        </button>
                    </div>
                </div>

            )
        }
    }
    class Com2 extends React.Component {
        state = {

        }
        render() {
            let { bost } = this.props
            let { arr } = this.props
            return (
                <div>
                    <h1>留言板</h1>
                    {
                        // map遍历arr的数据
                        arr.map((item, index) => {
                            return (
                                <div key={index}>
                                    <p>作者：{item.name}</p>
                                    <p>内容{item.age}</p>
                                    <button onClick={() => {
                                        bost(index)
                                    }}>
                                        删除 </button>
                                </div>
                            )
                        })
                    }
                </div>
            )
        }
    }
    ReactDOM.render(<App />, document.getElementById('root'))
</script>

</html>